<?php

/**
 * post_gallery_filter - stops [gallery] styles from being added to the page. making html invalid
 *
 * @since 0.3
 * @needsdoc
 */
function post_gallery_filter( $content ){ return ' '; }

/**
 * framework_nice_terms - clever terms
 *
 * @since 0.3
 * @needsdoc
 */
function framework_nice_terms( $term = '', $normal_separator = ', ', $penultimate_separator = ' and ', $end = '' ) {
	if ( !$term ) return;
	switch ( $term ):
		case 'cats':
			$terms = framework_get_terms( 'cats', $normal_separator );
			break;
		case 'tags':
			$terms = framework_get_terms( 'tags', $normal_separator );
			
			break;
	endswitch;
	if ( empty($term) ) return;
	$things = explode( $normal_separator, $terms );
	
	$thelist = '';
	$i = 1;
	$n = count( $things );
		
	foreach ( $things as $thing ) {
		
		$data = trim( $thing, ' ' );
		
		$links = preg_match( '/>(.*?)</', $thing, $link );
		$hrefs = preg_match( '/href="(.*?)"/', $thing, $href );
		$titles = preg_match( '/title="(.*?)"/', $thing, $title );
		$rels = preg_match( '/rel="(.*?)"/', $thing, $rel );
		
		if (1 < $i and $i != $n) {
			$thelist .= $normal_separator;
		}

		if (1 < $i and $i == $n) {
			$thelist .= $penultimate_separator;
		}
		$thelist .= '<a rel="'. $rel[1] .'" href="'. $href[1] .'"';
		if ( !$term = 'tags' )
			$thelist .= ' title="'. $title[1] .'"';
		$thelist .= '>'. $link[1] .'</a>';
		$i++;
	}
	$thelist .= $end;
	return apply_filters( 'framework_nice_terms', (string) $thelist );
}

/**
 * framework_cats_meow() - For category lists on category archives: Returns other categories except the current one (redundant)
 *
 * @since - 0.3
 * @usedby framework_entry_footer()
 */
function framework_get_terms( $term = NULL, $glue = ', ' ) {
	if ( !$term ) return;
	
	$separator = "\n";
	switch ( $term ):
		case 'cats':
			$current = single_cat_title( '', false );
			$terms = get_the_category_list( $separator );
			break;
		case 'tags':
			$current = single_tag_title( '', '',  false );
			$terms = get_the_tag_list( '', "$separator", '' );
			break;
	endswitch;
	if ( empty($terms) ) return;
	
	$thing = explode( $separator, $terms );
	foreach ( $thing as $i => $str ) {
		if ( strstr( $str, ">$current<" ) ) {
			unset( $thing[$i] );
			break;
		}
	}
	if ( empty( $thing ) )
		return false;

	return trim( join( $glue, $thing ) );
}

/**
 * include_all() - A function to include all files from a directory path
 *
 * @since - 0.3
 */
function include_all( $path, $ignore = false ) {

	/* Open the directory */
	$dir = @dir( $path ) or die( 'Could not open required directory ' . $path );
	
	/* Get all the files from the directory */
	while ( ( $file = $dir->read() ) !== false ) {
		/* Check the file is a file, and is a PHP file */
		if ( is_file( $path . $file ) and ( !$ignore or !in_array( $file, $ignore ) ) and preg_match( '/\.php$/i', $file ) ) {
			require_once( $path . $file );
		}
	}		
	$dir->close(); // Close the directory, we're done.
}

/**
 * file_scan() - Helper function to search for files based on given criteria
 *
 * @since - 0.3
 * @param string $path directory to search
 * @param array $ext file extensions
 * @param integer $depth depth of search
 * @param mixed $relative relative to which path
 * @return array paths of files found
 */

function file_scan( $path, $ext = false, $depth = 1, $relative = true ) {
	$files = array();
	_file_scan( trailingslashit( $path ), '', $ext, $depth, $relative, $files); // Scan for all matching files
	return $files;
}

/**
 * _file_scan() - Recursive function for framework_search
 *
 * @since - 0.3
 * @param string $base_path 
 * @param string $path 
 * @param string $ext 
 * @param string $depth 
 * @param mixed $relative 
 * @param string $files 
 * @return array paths of files found
 */
function _file_scan( $base_path, $path, $ext, $depth, $relative, &$files ) {
	if ( !empty( $ext ) ) {
		if ( !is_array( $ext ) ) {
			$ext = array( $ext );
		}
		$ext_match = implode( '|', $ext );
	}

	// Open the directory
	if(($dir = @dir( $base_path . $path ) ) !== false) {
		// Get all the files
		while( ( $file = $dir->read() ) !== false) {
			// Construct an absolute & relative file path
			$file_path = $path . $file;
			$file_full_path = $base_path . $file_path;

			// If this is a directory, and the depth of scan is greater than 1 then scan it
			if( is_dir( $file_full_path ) and $depth > 1 and !( $file == '.' or $file == '..' ) ) {
				_file_scan( $base_path, $file_path . '/', $ext, $depth - 1, $relative, $files );

			// If this is a matching file then add it to the list
			} elseif( is_file( $file_full_path ) and ( empty( $ext ) or preg_match( '/\.(' . $ext_match . ')$/i', $file ) ) ) {
				if ( $relative === true ) {
					$files[] = $file_path;
				} elseif ( $relative === false ) {
					$files[] = $file_full_path;
				} else {
					$files[] = str_replace( $relative, '', $file_full_path );
				}
			}
		}
		// Close the directory
		$dir->close();
	}
}
?>